package com.mh.mh.netty.protocal;


import io.netty.buffer.ByteBuf;
import io.netty.buffer.ByteBufUtil;
import io.netty.buffer.Unpooled;
import io.netty.channel.ChannelHandlerContext;
import io.netty.handler.codec.ByteToMessageDecoder;

import java.util.List;

/**
 * 自定义协议解码器 (字节转对象)
 *
 * @author stone
 * @date 2019/7/29 17:43
 */
public class LsDecoderMine extends ByteToMessageDecoder {
    /**
     * 报文开始的标志 header是int类型，占4个字节
     * 表示报文长度的contentLength是int类型，占4个字节
     */
    public final int BASE_LENGTH = 8;
    int type = 0;
    int length = 0;

    //记录上次未读完的字节
    private ByteBuf tempMessage = Unpooled.buffer();


    @Override
    protected void decode(ChannelHandlerContext ctx, ByteBuf in, List<Object> out) throws Exception {
        System.out.println("解码 --------->");
        int readableLen = in.readableBytes();
        System.out.println("=========收到" + readableLen + "字节========");
        ByteBuf inMessage;

        if(length==0){
            byte[] bytes = ByteBufUtil.getBytes(in.readBytes(4));
            type = DataUtils.bufdate(bytes);
            System.out.println("type >>> "+type);

            byte[] bytes2 = ByteBufUtil.getBytes(in.readBytes(4));
            length = DataUtils.bufdate(bytes2);
            System.out.println("length >>> "+length);
        }

//        //加上上次未读取完成的字节
//        if (tempMessage.readableBytes() == 0) {
//            inMessage = in;
//        }else {
//            inMessage = Unpooled.buffer();
//            inMessage.writeBytes(tempMessage);
//            inMessage.writeBytes(in);
//        }

//        int inMessageLen = inMessage.readableBytes();
//        System.out.println("inMessage >>>> "+inMessageLen);
        System.out.println("length >>> "+length);


        if (readableLen < length) {
            System.out.println("当前可读数据不够，继续等待");
            return;
        }

        byte[] databyteArry = ByteBufUtil.getBytes(in.readBytes(length));
        String str = new String(databyteArry);
        System.out.println("data >>> "+str);
        System.out.println("data.length() >>> "+str.length());

//        tempMessage.clear();







    }




//    @Override
//    protected void decode(ChannelHandlerContext ctx, ByteBuf in, List<Object> out) throws Exception {
//        System.out.println("解码 --------->");
//        System.out.println(in.readableBytes());
////        byte[] array = in.array();
//
//        byte[] bytes = ByteBufUtil.getBytes(in.readBytes(4));
//        int bufdate = DataUtils.bufdate(bytes);
//        System.out.println("ssssssssssssssssss >>> "+bufdate);
//
//        byte[] bytes2 = ByteBufUtil.getBytes(in.readBytes(4));
//        int length = DataUtils.bufdate(bytes2);
//        System.out.println("length >>> "+length);
//
//        byte[] databyteArry = ByteBufUtil.getBytes(in.readBytes(length));
//        String str = new String(databyteArry);
//        System.out.println(str);
//
//
//
////        byte[] bytes3 = ByteBufUtil.getBytes(in.readBytes(bufdate2*4));
////        int bufdate3 = DataUtils.bufdate(bytes3);
////        System.out.println("ssssssssssssssssss33 >>> "+bufdate3);
//
////        byte[] data = new byte[bufdate2];
////        in.readBytes(data);
////        MyLsProtocol protocol = new MyLsProtocol(data.length, data);
////        System.out.println("protocol >>>>>>>>>>> "+protocol);
////        out.add(protocol);
//
//
//
//
////        System.arraycopy(temp, 0, cmd, 0, 4);
////        if (com.mh.other.netty.socket.DataUtils.bufdate(cmd) == 301) {
////            isPort = true;
//////                System.arraycopy(temp, 0 ,size, 4, 4);
//////                resp = DataUtils.bufdate(size); //301后面的内容包大小
////        }
//
//
//
//
//        // 可读长度必须大于基本长度
//        // 防止socket字节流攻击
//        // 防止客户端传来的数据过大（太大的数据是不合理的）
////        if (in.readableBytes() > 2048) {
////            in.skipBytes(in.readableBytes());
////        }
//
////        for (int i = 0; i<8; i++){
////            char c = in.readChar();
////            System.out.println("cd    ---- "+c);
////        }
////        for (int i = 0; i<8; i++){
////            int cc = in.readInt();
////            System.out.println("cccccccc    ---- "+cc);
////        }
//
////
////        // 代码到这里，说明已经读到了报文标志
////        System.out.println("in:"+in);
////        int head = in.readInt();
////        System.out.println("head >>>> "+head);
////
////        // 消息长度
////        int length = in.readInt();
////        System.out.println("length >>>> "+length);
//
//
//
//        // 至此，读到一条完整报文
////        byte[] data = new byte[length];
////        in.readBytes(data);
////        MyLsProtocol protocol = new MyLsProtocol(data.length, data);
////        out.add(protocol);
//
//
//
////        byte[] data = new byte[8];
////        in.readBytes(data);
////        MyLsProtocol protocol = new MyLsProtocol(data.length, data);
////        out.add(protocol);
//    }
}
